/**
 *	\file
 *	\brief represents a hash table for class resources
 *
 * 	\date Aug 30, 2013
 * 	\author koldar
 */

#ifndef RESOURCEHASHTABLE_H_
#define RESOURCEHASHTABLE_H_

#include "uthash.h"
#include "ResourceHTCell.h"

#define __RESOURCE_HASH_TABLE_BUFFER_SIZE__ 50

/**typedef used to improve readability
 *
 */
typedef ResourceHTCell* ResourceHashTable;

/**uthash require that the hashtable is passed as double pointer when declared as formal parameter in a function.
 * So you have to declare the function:
 * \code
 * 		int add(PlocalResourceHTParameter phastable) {
 *			HASH_ADD(*phashtable,...,...); //good!
 * 		}
 * \endcode
 * and <b>NOT</b> like this:
 * \code
 * 		int add(PlocalResourceHT phastable){
 * 			HASH_ADD(phashtable,...,...); //bad!
 * 		}
 * \endcode
 *
 */
typedef ResourceHTCell** PresourceHashTable;

/**initialize correctly an hash table
 *
 * \pre
 *  \li phashtable must not be already used
 * \post
 *  \li phashtable is correctly initialized
 *
 * @param phashtable the hash table to initialize correctly
 */
void initResourceHashTable(PresourceHashTable phashtable);

/**find a resource in the hash table.
 *
 * \pre
 *  \li key not NULL;
 *  \li phashtable not NULL;
 *
 * @param phashtable the table to search into
 * @param key the key of the resource to look for
 * @return a pointer to the resource or NULL if not present
 */
PresourceElement findResourceInHashTable(PresourceHashTable phashtable,const char* key);

/**add a resource in the current hash table. phashtable might be NULL
 *
 * \pre
 *  \li resource is not present in the hash table
 *  \li resource must not be NULL
 *
 * @param phashtable the has table which will store the resource
 * @param resource the resource to store
 */
void addResourceInHashTable(PresourceHashTable phashtable, PresourceElement resource);

/**free a hash table correctly
 *
 * @param phashtable the hash table to be deallocated
 */
void freeResourceHashTable(PresourceHashTable phashtable);

/**generates a DOT sub graph representing this hash table.
 * If you are generating the class table (and if you are using this function you are probably doing it)
 * start parameters is an effective way to keep unique the names of the nodes. Let's assume that
 * start isn't present; if a class has 2 resources both of them will start the node numbers
 * from 0: this will generates different DOT nodes which have identical IDs; to avoid this you can easily set
 * start to avoid this. For example if the first function has 5 local resource while the second one has 4 you
 * will recall this function like this:
 *
 * \code
 * 	int id=0;
 * 	generateDOTResourceHashTable(f,phashtable_resource1,&id);
 * 	generateDOTResourceHashTable(f,phashtable_resource2,&id);
 * \endcode
 * The function will create various nodes (not necessary equal to the size of the hash table length)
 * and for each of them the variable in start will be incremented by one. So you
 * can reuse that variable for more than one call to avoid nodes with same IDs.
 *
 * \pre
 *  \li filename is a correct Linux file name;
 *  \li filename does not contain any "/" character;
 *  \li start must be greater or equal than zero
 * \post
 *  \li in f is appended a subgraph representing the hash table
 *
 * @param f represents the file to write on
 * @param phashtable the hash table to be drawn
 * @param start a pointer pointing to an area where the current DOT ID node is stored.
 */
void generateDOTResourceHashTable(FILE* f,PresourceHashTable phashtable,int* start);

#endif /* RESOURCETABLE_H_ */
